const SearchAlgorithms = require("../template")

function testAdvancedSearch() {
  const searcher = new SearchAlgorithms()

  console.log(
    "1. 插值搜索测试:",
    (() => {
      const uniformArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
      const testElements = [1, 7, 15, 25, 29]
      return testElements.every(target => {
        const result = searcher.interpolationSearch([...uniformArray], target)
        const expected = uniformArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "2. 指数搜索测试:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => i * 2)
      const testTargets = [0, 100, 500, 998]
      return testTargets.every(target => {
        const result = searcher.exponentialSearch([...largeArray], target)
        const expected = largeArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "3. 斐波那契搜索测试:",
    (() => {
      const fibArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
      const fibTestElements = [1, 7, 13, 25]
      return fibTestElements.every(target => {
        const result = searcher.fibonacciSearch([...fibArray], target)
        const expected = fibArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "4. 跳跃搜索测试:",
    (() => {
      const jumpArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
      const jumpTestElements = [1, 5, 10, 15, 20]
      return jumpTestElements.every(target => {
        const result = searcher.jumpSearch([...jumpArray], target)
        const expected = jumpArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "5. 三元搜索测试:",
    (() => {
      const ternaryArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]
      const ternaryTestElements = [1, 9, 15, 25, 29]
      return ternaryTestElements.every(target => {
        const result = searcher.ternarySearch([...ternaryArray], target)
        const expected = ternaryArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "6. 高级搜索算法一致性测试:",
    (() => {
      const testArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      const target = 9
      const algorithms = ["interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      const results = algorithms.map(algo => searcher[algo]([...testArray], target))
      return results.every(result => result === testArray.indexOf(target))
    })()
  )

  console.log(
    "7. 不存在的元素高级搜索测试:",
    (() => {
      const testArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      const target = 6
      const algorithms = ["interpolationSearch", "exponentialSearch", "fibonacciSearch", "jumpSearch", "ternarySearch"]
      return algorithms.every(algo => searcher[algo]([...testArray], target) === -1)
    })()
  )

  console.log(
    "8. this上下文高级搜索测试:",
    (() => {
      const advancedObj = {
        multiplier: 3,
        processAdvancedSearch: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.interpolationSearch(arr, target)
          return result * this.multiplier
        }
      }
      return advancedObj.processAdvancedSearch([1, 3, 5, 7, 9], 5) === 6 // 2 * 3 = 6
    })()
  )

  console.log(
    "9. 复杂对象this绑定高级搜索测试:",
    (() => {
      const advancedConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.exponentialSearch(arr, target)
          return result + this.config.offset
        }
      }
      return advancedConfigObj.processWithConfig([1, 3, 5, 7, 9], 3) === 6 // 1 + 5 = 6
    })()
  )
}

testAdvancedSearch()
